package com.example.whatrubbish.service.impl;

import com.example.whatrubbish.mapper.ReceiverAddressMapper;
import com.example.whatrubbish.model.ReceiverAddress;
import com.example.whatrubbish.service.ReceiverAddressService;
import com.example.whatrubbish.utils.DemoException;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ReceiverAddressServiceImpl implements ReceiverAddressService {
    @Autowired
    ReceiverAddressMapper receiverAddressMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return receiverAddressMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(ReceiverAddress record) throws DemoException {
        if (ObjectUtils.isNotEmpty(record.getId())) {
            return updateByPrimaryKeySelective(record);
        }
        record.setCreateTime(new Date());
        Integer isDefault = record.getIsDefault();
        if (isDefault == 0) {
            List<ReceiverAddress> receiverAddressList =  receiverAddressMapper.selectByUserId(record);
            if (!receiverAddressList.isEmpty()) {
                ReceiverAddress receiverAddress = receiverAddressList.get(0);
                receiverAddress.setIsDefault(1);
                receiverAddressMapper.updateByPrimaryKeySelective(receiverAddress);
            }
        } else {
            ReceiverAddress receiverAddress = new ReceiverAddress();
            receiverAddress.setUserId(record.getUserId());
            List<ReceiverAddress> receiverAddressList =  receiverAddressMapper.selectByUserId(record);
            if (receiverAddressList.isEmpty()) {
                record.setIsDefault(0);
            }
        }
        return receiverAddressMapper.insertSelective(record);
    }

    @Override
    public ReceiverAddress selectByPrimaryKey(Long id) {
        return receiverAddressMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ReceiverAddress record) throws DemoException {
        if (record.getIsDefault() == 1) {
            ReceiverAddress receiverAddress = new ReceiverAddress();
            receiverAddress.setUserId(record.getUserId());
            receiverAddress.setIsDefault(0);
            List<ReceiverAddress> receiverAddressList =  receiverAddressMapper.selectByUserId(receiverAddress);
            if (!receiverAddressList.isEmpty()) {
                throw new DemoException("至少保留一个默认地址");
            }
        }else {
            List<ReceiverAddress> receiverAddressList =  receiverAddressMapper.selectByUserId(record);
            receiverAddressList.forEach(item -> {
                item.setIsDefault(1);
                receiverAddressMapper.updateByPrimaryKeySelective(item);
            });
        }
        return receiverAddressMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<ReceiverAddress> getByUserId(String userId) {
        ReceiverAddress record = new ReceiverAddress();
        record.setUserId(userId);
        return receiverAddressMapper.selectByUserId(record);
    }
}
